package com.virgo.demo.support.controller.admin;

import com.virgo.common.entity.IResponse;
import com.virgo.common.entity.ListDataVo;
import com.virgo.demo.core.annotation.Log;
import com.virgo.demo.support.domain.bo.DictDataBo;
import com.virgo.demo.support.domain.vo.DictDataSelectVo;
import com.virgo.demo.support.domain.vo.DictDataVo;
import com.virgo.demo.support.service.DictDataService;
import com.virgo.limit.annotation.RepeatSubmit;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/manage168/dictData")
@RequiredArgsConstructor
public class DictDataController {

	private final DictDataService dictDataService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@Log(module = "字典值", type = "编辑", permission = "dict_data_save")
	@RepeatSubmit
	public IResponse<Void> saveAction(@Validated DictDataBo dictDataBo) {
		dictDataService.saveDictData(dictDataBo);
		return IResponse.ok();
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@Log(module = "字典值", type = "删除", permission = "dict_data_del")
	@RepeatSubmit
	public IResponse<Void> delAction(HttpServletRequest request) {
		String ids = IRequestUtils.getString(request,"ids","");
		if (ObjectUtils.isEmpty(ids)) {
			return IResponse.fail("参数错误");
		}
		dictDataService.deleteDictData(ids);
		return IResponse.ok();
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@Log(permission = "dict_data_list", saveLog = false)
	public IResponse<ListDataVo<DictDataVo>> listAction(HttpServletRequest request) {
		return IResponse.ok(dictDataService.findList(request));
	}

    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public IResponse<List<DictDataSelectVo>> selectAction(HttpServletRequest request) {
        String dictType = IRequestUtils.getString(request,"dictType","");
        if (ObjectUtils.isEmpty(dictType)) {
            return IResponse.fail("参数错误");
        }
        return IResponse.ok(dictDataService.dictDataList(dictType));
    }
}